import json
from flask import request, jsonify, send_from_directory, send_file, make_response

from src import common
from utils.initApp import mysql, default_response

from . import files
from .service import FilesService


@files.route('/upload', methods=['POST'])
def upload():
    data = request.form
    file = request.files["file"]
    token = common.get_token()

    values = dict()
    values["userId"] = token["userId"]
    values["md5"] = data["md5"]
    values["type"] = data["type"]
    values["table"] = data["table"]
    values["name"] = data["name"]
    values["parentId"] = data["parentId"]
    values["size"] = data["size"]
    values["index"] = data["index"]
    values["chunkSize"] = data["chunkSize"]
    values["flag"] = data["flag"] or common.create_id()

    result = filesService.upload(file, values)
    response = default_response.construct_response(result)
    return response


@files.route('/download/<file_id>', methods=['GET'])
def download(file_id):
    ranges = common.get_ranges()

    values = dict()
    values["id"] = file_id
    values["ranges"] = ranges

    result, code = filesService.download(values)
    response = default_response.construct_file_response(result)
    return response, code


@files.route('/create', methods=['PUT'])
def create():
    data = json.loads(request.get_data(as_text=True))
    token = common.get_token()

    values = dict()
    values["id"] = common.create_id()
    values["userId"] = token["userId"]
    values["name"] = data["name"]
    values["parentId"] = data["parentId"]

    result = filesService.create(values)
    response = default_response.construct_response(result)
    return response


@files.route('/listAll', methods=['GET'])
def list_all():
    token = common.get_token()

    values = dict()
    values['parentId'] = request.args.get('parentId')
    values['order'] = request.args.get('order')
    values['sort'] = request.args.get('sort')
    values['name'] = request.args.get('name')
    values['type'] = request.args.get('type')
    values['userId'] = token["userId"]

    result = filesService.list_all(values)
    response = default_response.construct_response(result)
    return response


@files.route('/rename', methods=['POST'])
def rename():
    data = json.loads(request.get_data(as_text=True))

    values = dict()
    values['parentId'] = data['parentId']
    values['id'] = data['id']
    values['name'] = data['name']

    result, msg, code = filesService.rename(values)
    response = default_response.construct_response(result, msg, code)
    return response


@files.route('/delete', methods=['DELETE'])
def delete():
    data = json.loads(request.get_data(as_text=True))

    values = dict()
    values['ids'] = data['ids']

    result = filesService.delete(values)
    response = default_response.construct_response(result)
    return response


@files.route('/move', methods=['POST'])
def move():
    data = json.loads(request.get_data(as_text=True))

    values = dict()
    values['targetId'] = data['targetId']
    values['ids'] = data['ids']

    result = filesService.move(values)
    response = default_response.construct_response(result)
    return response


@files.route('/detail/<file_id>', methods=['POST'])
def detail(file_id):
    data = json.loads(request.get_data(as_text=True))

    values = dict()
    values["id"] = file_id

    result = filesService.detail(values)
    response = default_response.construct_response(result)
    return response


filesService = FilesService([])
